#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 3) {
        cout << "Usage: " << argv[0] << " in_file num" << endl;
        return 0;
    }

    int *array;
    int elem_num;
    bool ret_code;

    string file_name(argv[1]);
    string str_num(argv[2]);
    ret_code = readInput(file_name.c_str(), 0, array, elem_num);
    if (ret_code == false) {
        cout << "Read file " << file_name << " error!" << endl;
        return 1;
    }
    
    int num = atoi(str_num.c_str());

    cout << "Original array:" << endl;
    printArray(array, elem_num);

    quickSort(array, elem_num);

    cout << "After sort, the array is:" << endl;
    printArray(array, elem_num);

    int index, index1;
    index = findElem0(array, elem_num, num);
    if (index == -1) {
        cout << "Elem " << num << " is not found!" << endl;
    }
    else {
        cout << "Index of " << num << " is " << index << "." << endl;
    }

    index1 = findElem1(array, elem_num, num);
    if (index1 == -1) {
        cout << "Elem " << num << " is not found!" << endl;
    }
    else {
        cout << "Index of " << num << " is " << index1 << "." << endl;
    }

    ret_code = rotateArray(array, elem_num, 3);
    if (ret_code == false) {
        cout << "Rotate array error!" << endl;
        return 1;
    }

    cout << "After rotation, the array is:" << endl;
    printArray(array, elem_num);

    index = findElem(array, elem_num, num);
    if (index == -1) {
        cout << "Elem " << num << " is not found!" << endl;
    }
    else {
        cout << "Index of " << num << " is " << index << "." << endl;
    }

    delete array;

    return 0;
}

